<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <canvas id="dashed" width="500" height="500"></canvas>
  <script>
    const canvas = document.querySelector('#dashed')
    const ctx = canvas.getContext('2d')

    // ctx.beginPath()
    // ctx.moveTo(100, 100)
    // ctx.lineTo(110, 100)
    // ctx.stroke()
    // ctx.closePath()

    // ctx.beginPath()
    // ctx.moveTo(120, 100)
    // ctx.lineTo(130, 100)
    // ctx.stroke()
    // ctx.closePath()

    drawDashed(100, 100, 110, 100, 50, 20, 0)
    drawDashed(100, 100, 102, 102, 50, 15, 5)
    drawDashed(100, 100, 102, 102, 50, 35, 5)
    drawDashed(100, 100, 110, 110, 50, 20, 20)
    drawDashed(100, 100, 100, 110, 50, 0, 20)

    function drawDashed(x1, y1, x2, y2, count, stepX, stepY) {
      for (let i = 0; i < count; i++) {
        ctx.beginPath()
        ctx.moveTo(x1 + stepX * i, y1 + stepY * i)
        ctx.lineTo(x2 + stepX * i, y2 + stepY * i)
        ctx.stroke()
        ctx.closePath()
      }
    }
  </script>
</body>

</html>